package net.huashitong.supervision.task.service.SpecialService;

import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.service.DeptService;
import com.sirdc.modules.utils.StringUtils;
import com.sirdc.modules.utils.date.DateUtils;
import net.huashitong.supervision.task.entity.MajorBack;
import net.huashitong.supervision.task.entity.SpecialTask;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.supervision.task.service.MajorBackService;
import net.huashitong.supervision.task.service.SpecialTaskService;
import net.huashitong.supervision.task.service.TaskMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 特殊任务的处理流程：例如 单位重大事项，单位内部事项
 * 
 */
@Service
public class TaskSpecialService {

    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private SpecialTaskService specialTaskService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private MajorBackService majorBackService;


    //督办单位反馈任务。并且评价当前任务
    public int dubanFeedBack(TaskMainFilter filter){

        
        //先取出主任务
        TaskMain taskMain = taskMainService.getById(filter.getSysId());
        if (StringUtils.isBlank(filter.getMainResultFile())) {
            System.out.println("请上传汇总附件");;
            return 1;
        }

        //在取出单位重大事项；
        //taskMain.geto
        SpecialTask specialTask = specialTaskService.getById(filter.getSpecialId());
        //将汇总的结果更新
        taskMain.setMainResult(filter.getMainResult());
        taskMain.setMainResultFile(filter.getMainResultFile());
        taskMain.setMainResultFileName(filter.getMainResultFileName());
        //设置主任务任务为待办任务；yifankui
        taskMain.setStatus("3");
        taskMain.setOverdueDeptState(null);
        //更新主任务
        taskMainService.update(taskMain);
        MajorBack majorBack = new MajorBack();
        majorBack.setMainResult(filter.getMainResult());
        majorBack.setMainResultFile(filter.getMainResultFile());
        majorBack.setMainResultFileName(filter.getMainResultFileName());
        majorBack.setDateTime(DateUtils.getDateTime());
        SysDept dept = deptService.getById(taskMain.getOverdueDept());
        majorBack.setDeptId(dept.getSysId());
        majorBack.setDeptName(dept.getDeptName());
        majorBack.setTaskMainId(taskMain.getSysId());
        majorBack.setState("0");
        //设置特殊事项id
        majorBack.setSpecialId(filter.getSpecialId());
        majorBack.setJsonOrder(filter.getJson());

        if (StringUtils.isNotBlank(filter.getSpecialRound())){
            majorBack.setTimeRound(filter.getSpecialRound());
        }else{
            majorBack.setTimeRound(specialTask.getTimeRound());
        }
        majorBackService.save(majorBack);

        return 0;

    }

}
